package cn.lijigou.shopping.service;

import java.io.PrintWriter;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.concurrent.ExecutorService;

import cn.lijigou.shopping.MActivity;
import cn.lijigou.shopping.MApplication;
import cn.lijigou.shopping.WelcomeActivity;
import cn.lijigou.shopping.domain.MLocation;
import cn.lijigou.shopping.util.GetSingleThreadPool;


import android.content.Context;
import android.os.Build;
import android.util.Log;

public class CrashHandler implements UncaughtExceptionHandler {
	private Context mContext;   
    private Thread.UncaughtExceptionHandler mDefaultHandler;   
    private static CrashHandler INSTANCE; 
	private ExecutorService executorService = GetSingleThreadPool.getInstance();
	
	
	public void uncaughtException(Thread thread, Throwable ex) {
		final String mErrorLog = MApplication.mLog.toString();
		executorService.execute(new Thread() {
			public void run() {
				MSystem.setLog(" @IMEI=" + MApplication.IMEI + " @Device=" + Build.MODEL + " @Version=" + Build.VERSION.RELEASE
						+ "@SoftWareVersion=" + MApplication.SoftWareVersion + "\n" + mErrorLog + "\n");
			}
		});
		MApplication.mLog.reverse();
//		MActivity.killMyProcess();
	}
	 public void init(Context ctx) {   
	        mContext = ctx;   
	        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();   
	        Thread.setDefaultUncaughtExceptionHandler(this);   
	}   
	       
	public static CrashHandler getInstance() {   
	    if (INSTANCE == null) {   
	        INSTANCE = new CrashHandler();
	    }   
	    return INSTANCE;   
	} 
}
